package com.hyt.it.ogt.opertaion.track.mq;

import com.hyt.it.ogt.opertaion.track.config.OperationTrackMqProperties;
import com.hyt.it.ogt.opertaion.track.config.OperationTrackPropertyFactory;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 消息队列配置
 *
 * @author 刘恒活
 * @since 2020/7/10 16:18
 */

@Configuration
@Slf4j
@ConditionalOnClass({Channel.class, RabbitTemplate.class, RabbitAdmin.class})
@PropertySource(value = "classpath:application-track.yml", factory = OperationTrackPropertyFactory.class)
@ConfigurationProperties(prefix  = "mq.operationtrackdirect")
public class OperationTrackRabbitMqConfig {

    @Bean
    public OperationTrackMqProperties operationTrackMqProperties(@Value("${mq.operationtrackdirect.kwOperationTrackWarnExchange}") String kwOperationTrackWarnExchange,
                                                                 @Value("${mq.operationtrackdirect.kwOperationTrackWarnQueue}") String kwOperationTrackWarnQueue,
                                                                 @Value("${mq.operationtrackdirect.kwOperationTrackWarnRoute}") String kwOperationTrackWarnRoute,
                                                                 @Value("${mq.operationtrackdirect.kwOperationTrackHandUpExchange}") String kwOperationTrackHandUpExchange,
                                                                 @Value("${mq.operationtrackdirect.kwOperationTrackHandUpQueue}") String kwOperationTrackHandUpQueue,
                                                                 @Value("${mq.operationtrackdirect.kwOperationTrackHandUpRoute}") String kwOperationTrackHandUpRoute,
                                                                 @Value("${mq.operationtrackdirect.ksOperationTrackAnswerExchange}") String ksOperationTrackAnswerExchange,
                                                                 @Value("${mq.operationtrackdirect.ksOperationTrackAnswerQueue}") String ksOperationTrackAnswerQueue,
                                                                 @Value("${mq.operationtrackdirect.ksOperationTrackAnswerRoute}") String ksOperationTrackAnswerRoute,
                                                                 @Value("${mq.operationtrackdirect.ksOperationTrackOtherExchange}") String ksOperationTrackOtherExchange,
                                                                 @Value("${mq.operationtrackdirect.ksOperationTrackOtherQueue}") String ksOperationTrackOtherQueue,
                                                                 @Value("${mq.operationtrackdirect.ksOperationTrackOtherRoute}") String ksOperationTrackOtherRoute) {
        OperationTrackMqProperties properties = new OperationTrackMqProperties();
        properties.setKwOperationTrackWarnExchange(kwOperationTrackWarnExchange);
        properties.setKwOperationTrackWarnQueue(kwOperationTrackWarnQueue);
        properties.setKwOperationTrackWarnRoute(kwOperationTrackWarnRoute);
        properties.setKwOperationTrackHandUpExchange(kwOperationTrackHandUpExchange);
        properties.setKwOperationTrackHandUpQueue(kwOperationTrackHandUpQueue);
        properties.setKwOperationTrackHandUpRoute(kwOperationTrackHandUpRoute);
        properties.setKsOperationTrackAnswerExchange(ksOperationTrackAnswerExchange);
        properties.setKsOperationTrackAnswerQueue(ksOperationTrackAnswerQueue);
        properties.setKsOperationTrackAnswerRoute(ksOperationTrackAnswerRoute);
        properties.setKsOperationTrackOtherExchange(ksOperationTrackOtherExchange);
        properties.setKsOperationTrackOtherQueue(ksOperationTrackOtherQueue);
        properties.setKsOperationTrackOtherRoute(ksOperationTrackOtherRoute);
        return properties;
    }

    @Bean
    public DirectExchange kwOperationTrackWarnExchange(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Exchange exchange = ExchangeBuilder.directExchange(operationTrackMqProperties.getKwOperationTrackWarnExchange()).durable(true).build();
        rabbitAdmin.declareExchange(exchange);
        return (DirectExchange) exchange;
    }

    @Bean
    public DirectExchange kwOperationTrackHandUpExchange(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Exchange exchange = ExchangeBuilder.directExchange(operationTrackMqProperties.getKwOperationTrackHandUpExchange()).durable(true).build();
        rabbitAdmin.declareExchange(exchange);
        return (DirectExchange) exchange;
    }

    @Bean
    public DirectExchange ksOperationTrackAnswerExchange(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Exchange exchange = ExchangeBuilder.directExchange(operationTrackMqProperties.getKsOperationTrackAnswerExchange()).durable(true).build();
        rabbitAdmin.declareExchange(exchange);
        return (DirectExchange) exchange;
    }

    @Bean
    public DirectExchange ksOperationTrackOtherExchange(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Exchange exchange = ExchangeBuilder.directExchange(operationTrackMqProperties.getKsOperationTrackOtherExchange()).durable(true).build();
        rabbitAdmin.declareExchange(exchange);
        return (DirectExchange) exchange;
    }

    @Bean
    public Queue kwOperationTrackWarnQueue(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Queue queue = new Queue(operationTrackMqProperties.getKwOperationTrackWarnQueue());
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Queue kwOperationTrackHandUpQueue(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Queue queue = new Queue(operationTrackMqProperties.getKwOperationTrackHandUpQueue());
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Queue ksOperationTrackAnswerQueue(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Queue queue = new Queue(operationTrackMqProperties.getKsOperationTrackAnswerQueue());
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    public Queue ksOperationTrackOtherQueue(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        Queue queue = new Queue(operationTrackMqProperties.getKsOperationTrackOtherQueue());
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    @Bean
    Binding operationTrackWarnBinding(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        return BindingBuilder.bind(this.kwOperationTrackWarnQueue(rabbitAdmin, operationTrackMqProperties))
                .to(kwOperationTrackWarnExchange(rabbitAdmin, operationTrackMqProperties))
                .with(operationTrackMqProperties.getKwOperationTrackWarnRoute());
    }

    @Bean
    Binding operationTrackHandUpBinding(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        return BindingBuilder.bind(this.kwOperationTrackHandUpQueue(rabbitAdmin, operationTrackMqProperties))
                .to(kwOperationTrackHandUpExchange(rabbitAdmin, operationTrackMqProperties))
                .with(operationTrackMqProperties.getKwOperationTrackHandUpRoute());
    }

    @Bean
    Binding operationTrackAnswerBinding(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        return BindingBuilder.bind(this.ksOperationTrackAnswerExchange(rabbitAdmin, operationTrackMqProperties))
                .to(ksOperationTrackAnswerExchange(rabbitAdmin, operationTrackMqProperties))
                .with(operationTrackMqProperties.getKsOperationTrackAnswerRoute());
    }

    @Bean
    Binding operationTrackOtherBinding(RabbitAdmin rabbitAdmin, OperationTrackMqProperties operationTrackMqProperties) {
        return BindingBuilder.bind(this.ksOperationTrackOtherExchange(rabbitAdmin, operationTrackMqProperties))
                .to(ksOperationTrackOtherExchange(rabbitAdmin, operationTrackMqProperties))
                .with(operationTrackMqProperties.getKsOperationTrackOtherRoute());
    }

}
